package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：HX
 * @date ：Created in 2019/12/11
 * @description ：
 * @version: 1.0
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Menu> menuPage=menuDao.findPage(queryPageBean.getQueryString());
        return new PageResult(menuPage.getTotal(),menuPage.getResult());
    }

    @Override
    public void add(Menu menu) {
        if (menu.getLevel()==1){
//            添加主菜单
//            查询主菜单优先级是否存在
            Long count=menuDao.findPriority(menu);
            if (count>0){
                throw new RuntimeException(MessageConstant.ADD_MENU_FAIL+"请更改优先级！！！");
            }else {
                Integer priority = menu.getPriority();
                menu.setPath(Integer.toString(priority+1));
                menuDao.add(menu);
            }
        }else {
//            添加子菜单
//            查询子菜单优先级是否存在
            Long count = menuDao.findPriority(menu);
            if (count>0){
                throw new RuntimeException(MessageConstant.ADD_MENU_FAIL+"请更改优先级！！！");
            }else {
                Integer i = menu.getPriority();
                String path=menuDao.findPathByParentId(menu.getParentMenuId());
                menu.setPath("/"+path+"-"+i);

                menuDao.add(menu);
            }
        }
    }

    @Override
    public List<Menu> findALlParentMenu() {
        return menuDao.findALlParentMenu();
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        //            编辑主菜单
        Menu DBmenu = menuDao.findById(menu.getId());
        Long count = menuDao.findPriority(menu);
        if (menu.getLevel()==1){
//            通过Id查询本菜单
            if (DBmenu.getLevel()==1){
//                主菜单到主菜单的更改
                if (count>0){
                    if (DBmenu.getPriority()==menu.getPriority()){
                        menuDao.edit(menu);
                    }else {
                        throw new RuntimeException(MessageConstant.EDIT_MENU_FAIL+"!该优先级已存在菜单，请换个优先级！！！");
                    }
                }else {
                    menu.setPath(Integer.toString(menu.getPriority()+1));
                    menuDao.edit(menu);
                }
            }else {
//                从子菜单转到主菜单
                if (count>0){
                    throw new RuntimeException(MessageConstant.EDIT_MENU_FAIL+"!该优先级已存在菜单，请换个优先级！！！");
                }else {
                    menu.setPath(Integer.toString(menu.getPriority()+1));
                    menuDao.edit(menu);
                }
            }
        }else {
            if (DBmenu.getLevel()==1){
//                主菜单转子菜单
                throw new RuntimeException(MessageConstant.EDIT_MENU_FAIL+"!主菜单不能转换成子菜单，请更换其他主菜单优先级！！！");
            }else {
//                 子菜单转子菜单
                if (menu.getParentMenuId()!=DBmenu.getParentMenuId()){
//                    子菜单转其他父类子菜单
                    if (count>0){
                        throw new RuntimeException(MessageConstant.EDIT_MENU_FAIL+"!该优先级已存在菜单，请换个优先级！！！");
                    }else {
                        Integer i = menu.getPriority();
                        String path=menuDao.findPathByParentId(menu.getParentMenuId());
                        menu.setPath("/"+path+"-"+i);
                        menuDao.edit(menu);
                    }
                }else {
//                    子菜单转自己父类子菜单
                    if (count>0){
                        if (DBmenu.getPriority()==menu.getPriority()){
                            menuDao.edit(menu);
                        }else {
                            throw new RuntimeException(MessageConstant.EDIT_MENU_FAIL+"!该优先级已存在菜单，请换个优先级！！！");
                        }
                    }else {
                        Integer i = menu.getPriority();
                        String path=menuDao.findPathByParentId(menu.getParentMenuId());
                        menu.setPath("/"+path+"-"+i);
                        menuDao.edit(menu);
                    }
                }
            }

        }
    }

    @Override
    public void deleteById(Integer id) {
        Menu DBmenu = menuDao.findById(id);
        if (DBmenu.getLevel()==2){
//            删除子类菜单
//            查询是否和角色关联
            Long associationCount= menuDao.findCountByMenuId(id);
            if (associationCount>0){
                throw new RuntimeException(MessageConstant.MENU_HAS_ASSOCIATION);
            }
            menuDao.deleteById(id);
        }else {
//            删除父类菜单
//            查询父类菜单下的子类菜单是否和角色关联
            Long associationCount2= menuDao.findCountByChildrenMenuId(id);
            if (associationCount2>0){
                throw new RuntimeException(MessageConstant.CHILDRENMENU_HAS_ASSOCIATION);
            }
//            查询父类菜单是否和角色关联
            Long associationCount= menuDao.findCountByMenuId(id);
            if (associationCount>0){
                throw new RuntimeException(MessageConstant.MENU_HAS_ASSOCIATION);
            }
//            先删除父类下的子菜单
            menuDao.deleteChildrenByParentId(id);
//            最后删除父类菜单
            menuDao.deleteById(id);
        }
    }
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }
}
